package com.zhao.sqltest;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.zhao.database.MyOpenHelper;

import org.w3c.dom.Text;

import java.util.LinkedList;

public class MainActivity extends Activity {
    private static SQLiteDatabase db;
    private int rawCount;//表的行数
    private ListView lv;
    private Cursor cursor;
    private ListAdapter adapter;
    private LinkedList<Contacter> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    /**
     * 初始化
     */
    protected void init() {
        MyOpenHelper oh = new MyOpenHelper(MainActivity.this, "contacter.db", null, 1);
        db = oh.getWritableDatabase();
        lv=(ListView)findViewById(R.id.lv);
        list=new LinkedList<Contacter>();
        cursor = db.query("contacter", new String[]{"name", "phone", "_id"}, null, null, null, null, "name");
        while(cursor.moveToNext()) {
            Contacter contacter=new Contacter(
                    cursor.getString(0),
                    cursor.getString(1),
                    cursor.getString(2)
            );
            list.add(contacter);
        }
        disContacter();
    }

    /**
     * 显示从数据库读取的联系人结果 数据库Query
     * 循环动态创建TextView
     * 为每一条记录前添加一个删除按键 对数据库内数据delete
     * 长按单条联系人记录进入修改 对数据库内数据Updata
     * 短按单条联系人 转到虚拟按键 拨号或发送短信
     *
     */
    public void disContacter() {
        cursor.moveToFirst();
        rawCount=cursor.getCount();

        adapter=new BaseAdapter() {
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                final Contacter contacter=list.get(position);
                LinearLayout layout=new LinearLayout(MainActivity.this);
                layout.setOrientation(LinearLayout.HORIZONTAL);
                TextView tv= new TextView(MainActivity.this);
                Button bt=new Button(MainActivity.this);
                tv.setText(contacter.toString());
                tv.setTextSize(18);
                tv.setLongClickable(true);
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + contacter.getPhone()));
                        startActivity(intent);
                    }
                });
                tv.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        Intent intent = new Intent(MainActivity.this, UpdataDataActivity.class);
                        intent.putExtra("name", contacter.getName());
                        intent.putExtra("phone", contacter.getPhone());
                        intent.putExtra("_id", contacter.get_id());
                        startActivity(intent);
                        return false;
                    }
                });
                bt.setText("删除");
                bt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        db.delete("contacter", "_id=?",
                                new String[]{contacter.get_id()});
                        init();
                    }
                });
                layout.addView(tv);
                layout.addView(bt);
                return layout;
            }
        };
        lv.setAdapter(adapter);
    }
    /**
     * 添加新的联系人
     * @param v
     */
    public void insertNewContacter(View v){
        Intent intent=new Intent(MainActivity.this,InsertDataActivity.class);
        startActivity(intent);
        disContacter();
    }

    public static SQLiteDatabase getDb() {
        return db;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        db.close();
    }

    @Override
    protected void onRestart() {
        init();
        super.onRestart();
    }


}
